package com.tsd.system.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysRoleResourceMapper;
import com.tsd.system.dao.SysUserRoleMapper;
import com.tsd.system.entity.SysRoleResource;
import com.tsd.system.entity.SysRoleResourceExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysRoleResourceService;
import com.tsd.system.service.SysUserResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRoleResource 服务实现层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Service
public class SysRoleResourceServiceImpl extends BaseServiceImpl implements SysRoleResourceService {

    @Resource
    private SysRoleResourceMapper sysRoleResourceMapper;

    @Override
    public SysRoleResource getSysRoleResourceById(Long id) throws Exception {
        return sysRoleResourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveSysRoleResource(SysRoleResource record, SysUser sysUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            SysRoleResource old = sysRoleResourceMapper.selectByRoleResource(record);
            if (old != null) {
                record.setId(old.getId());
                sysRoleResourceMapper.updateByPrimaryKeySelective(record);
            } else {
                super.autoInjectBaseData(record, sysUser, TYPE_CREATE);
                sysRoleResourceMapper.insertSelective(record);
            }
        } else {
            sysRoleResourceMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void deleteSysRoleResource(Long id) throws Exception {
        sysRoleResourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SysRoleResource> findSysRoleResources(Map<String, Object> params, PageBean page) throws Exception {
        return sysRoleResourceMapper.selectByPage(params, page);
    }

    @Override
    public void deleteSysRoleResource(SysUser opUser, String roleId, List<String> resourceIdList) throws Exception {
        SysRoleResource t = new SysRoleResource();
        for (String id : resourceIdList) {
            t.setRole_sid(roleId);
            t.setResource_sid(id);
            SysRoleResource old = sysRoleResourceMapper.selectByRoleResource(t);
            if (old != null) {
                sysRoleResourceMapper.deleteByPrimaryKey(old.getId());
            }
        }
    }

    @Override
    public void saveSysResourceList2Role(SysUser opUser, String roleId, List<String> resourceIdList, List<SysRoleResource> resourceList) throws Exception {
        List<SysRoleResource> old = findRoleResourcesByRoleId(roleId);
        List<SysRoleResource> updateList = new ArrayList<>();
        List<Long> delIds = new ArrayList<Long>();
        for (SysRoleResource roleResource : old) {
            if (!resourceIdList.contains(roleResource.getResource_sid())) {
                delIds.add(roleResource.getId());
            } else {
                SysRoleResource n = ListUtil.get(resourceList, t -> t.getResource_sid().equals(roleResource.getResource_sid()));
                if (n != null && !HlpUtils.isEqual(n.getCheck_type(), roleResource.getCheck_type())) {
                    n.setId(roleResource.getId());
                    updateList.add(n);
                }
            }
        }
        if (updateList.size() > 0) {
            sysRoleResourceMapper.updateList(updateList);
        }
        if (!delIds.isEmpty()) {
            //2020-09-27 改为批量删除
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo();
            logicVo.setIdList(delIds);
            sysRoleResourceMapper.deleteByList(logicVo);
        }
        // 2020-09-27 改为批量操作
        //查询角色已有权限
        List<SysRoleResource> validList = sysRoleResourceMapper.selectByRoleSid(roleId);
        List<String> validResuorce = ListUtil.map(validList, SysRoleResource::getResource_sid);
        List<String> insertList = ListUtil.filter(resourceIdList, t -> !validResuorce.contains(t));
        if (!HlpUtils.isEmptyList(insertList)) {
            List<SysRoleResource> roleResourceList = new ArrayList<>();
            for (String resSid : insertList) {
                SysRoleResource resource = new SysRoleResource();
                resource.setRole_sid(roleId);
                resource.setResource_sid(resSid);
                for (SysRoleResource res : resourceList) {
                    if (res.getResource_sid().equals(resSid)) {
                        resource.setCheck_type(res.getCheck_type());
                        break;
                    }
                }
                super.autoInjectBaseData(resource, opUser, this.TYPE_CREATE);
                roleResourceList.add(resource);
                if (roleResourceList.size() == 200) {
                    sysRoleResourceMapper.insertList(roleResourceList);
                    roleResourceList.clear();
                }
            }
            if (!HlpUtils.isEmptyList(roleResourceList)) {
                sysRoleResourceMapper.insertList(roleResourceList);
            }
        }
    }

    @Override
    public List<SysRoleResource> findRoleResourcesByRoleId(String roleId) throws Exception {
        return sysRoleResourceMapper.selectByRoleSid(roleId);
    }

    @Override
    public List<SysRoleResourceExt> queryValidByRoleList(List<String> roleList) throws Exception {
        return sysRoleResourceMapper.selectValidByRoleList(roleList);
    }
}
